jQuery(function($){
	var prodStockCheck={
			grid : null,
			init:function(){
				if (roles.length > 0) {
					var add = [];
					var items = [];				
					for(var i=0; i<roles.length; i++) {
			    		if (roles[i] == 'GOOD_PERSON' || roles[i] == 'SYSTEM_ADMIN' || roles[i] == 'GOODS_SUPERVISOR') {
			    			add = [{ text: "同步",
			    				     id:"add", 
			    				     click: function(){	
			    				    	 var rows=grid.getSelectedRows();
		    				    		 var ids=[];
			    				    	 if(rows && rows.length){
			    				    		 for(var x  in rows){
			    				    			 if(!rows[x].isSynchronization){
			    				    				 ids.push(rows[x].uniqueId);
			    				    			 }
			    				    		 }
			    				    		 if(ids.length){
			    				    			 $.ligerDialog.confirm('确定要同步数据？', function (yes) { 
			    				    				 if(yes){
					    				    			 prodStockCheck.synchronizationToSys(ids);
			    				    				 }
			    				    			 });
			    				    		 }else{
				    				    		 window.parent.f_tip("请选择未同步的数据"); 
			    				    		 }
			    				    	 }else{
			    				    		 window.parent.f_tip("请选择需要同步的数据");
			    				    	 }
			    			         }, 
			    			         icon: "add"},{ line: true }];			    			
			    		}
			    	}
					items = items.concat(add);
					toolbar = {items : items};
				}
				this.pageList();
			},
			pageList:function(){
				prodStockCheck.grid = $("#maingrid").ligerGrid({
		            columns: [
		            /*{ display: 'ID', name: 'id'},*/
		            { display: '仓库编号', name: 'whNo'},
		            { display: '供应商编号', name: 'supAccount'},
		            { display: '货品编号', name: 'prodNo'},
		            { display: '商品编号', name: 'commoNo'},
		            { display: '商品数量', name: 'productTotal'},
		            { display: '系统商品数量', name: 'sysProductTotal'},
		            { display: '良品数量', name: 'qualifiedQuantity'},
		            { display: '系统良品数量', name: 'sysQualifiedQuantity'},
		            { display: '次品数量', name: 'defectiveQuantity'},
		            { display: '系统次品数量', name: 'sysDefectiveQuantity'},
		            /*{ display: '系统可用数量', name: 'preOccupyQuantity',render: function (rowdata, rowindex, value){
		            	return (rowdata.sysQualifiedQuantity-(value?value:0))+"";
		            }},*/
		            { display: '是否系统数据', name: 'isExist', render: function (rowdata, rowindex, value){
		            	if(value){
		            		return "是";
		            	}else{
		            		return "否";
		            	}
		            }},
		            { display: '操作',width:'10%', isSort: false, render: function (rowdata, rowindex, value){
		            	if(!rowdata.isSynchronization){
		            		return "<a href=javascript:synchronizationToSys('" + rowdata.uniqueId + "')>同步</a> ";
		            	} else {
		            		return "已同步";
		            	}
		            }}], 
		            pageSize:30,
		            width: '100%',
		            height: '100%',
		            dataType: 'server', 
		            dataAction: 'server',
		            pagesizeParmName: 'pageSize',
		            parms: [],
					url: "checkProductStockPageList.do",
					toolbar: toolbar,
					checkbox: true
		        });
				grid=prodStockCheck.grid;
			},
			synchronizationToSys:function(ids){
				$.ajax({
			   		 type:'POST',
			   		 url:basePath+"/product/productmgt/synchronizationToSys.do",
			   	     data:{"ids":JSON.stringify(ids)},
			   		 success : function(data){
			   			 if(data == 'success'){
					        window.parent.f_tip('同步成功');	
					        prodStockCheck.reload();
					        //window.top.tab.reload($.trim($("#pTabid").val()));
			   		     }else if(data == 'fail'){
			          		window.parent.f_tip('同步失败');
			   		     }
			   	   	}
		  		});
			},
			reload:function(){
				prodStockCheck.grid.set({
					url: "checkProductStockPageList.do"						
				});
				prodStockCheck.grid.loadData();
			}
	};
	prodStockCheck.init();
}); 